﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class shop_PrepareGoods : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        SqlConnection sqlcon;
        SqlCommand sqlcom;

        String strConnection = "Server=.;Database=keshe2;Integrated Security=SSPI";
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();

        String sql = "SELECT 订单.订单编号, 客户.客户名, 商品.商品名称, 订单.数量,订单.总价, 订单.创建时间, 订单.订单状态,订单.收货状态 FROM 订单 INNER JOIN 客户 ON 订单.客户编号 = 客户.客户编号 INNER JOIN 商品 ON 订单.商品编号 = 商品.商品编号";
        sqlcon = new SqlConnection(strConnection);
        sqlcon.Open();
        sqlcom = new SqlCommand(sql, sqlcon);
        SqlDataReader Dr = sqlcom.ExecuteReader();
        int a = 0;
        try
        {
            if (TextBox1.Text.Trim().Length != 0)
            {
                while (Dr.Read())
                {
                    if (Dr[DropDownList1.SelectedValue].ToString().Contains(TextBox1.Text))
                    {
                        GridView1.DataSourceID = "SqlDataSource1";
                        string sql1 = "SELECT 订单.订单编号, 客户.客户名, 商品.商品名称, 订单.数量,订单.总价, 订单.创建时间, 订单.订单状态 FROM 订单 INNER JOIN 客户 ON 订单.客户编号 = 客户.客户编号 INNER JOIN 商品 ON 订单.商品编号 = 商品.商品编号 AND " + DropDownList1.SelectedValue + " LIKE '%" + TextBox1.Text + "%'";
                        SqlDataSource1.SelectCommand = sql1;
                        SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                        a = 1;
                        break;
                    }

                }
                if (a == 0)
                {
                    GridView1_empty();
                }
            }
        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message.ToString());
        }
        Dr.Close();
        sqlcon.Close();
    }
    protected void GridView1_empty()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("订单编号");
        dt.Columns.Add("客户名");
        dt.Columns.Add("商品名称");
        dt.Columns.Add("数量");
        dt.Columns.Add("总价");
        dt.Columns.Add("创建时间");
        dt.Columns.Add("订单状态");
        dt.Columns.Add("收货状态");
        if (dt.Rows.Count == 0)
        {
            dt.Rows.Add(dt.NewRow());
            GridView1.DataSourceID = "";
            GridView1.DataSource = dt;
            GridView1.DataBind();
            int columnCount = dt.Columns.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
            GridView1.Rows[0].Cells[0].Text = "暂无该商品记录！";
            GridView1.Rows[0].Cells[0].Style.Add("text-align", "center");
        }

    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString());
    }
}